package com.example.arithmeticleetcode.leetcode.nov;

import java.util.Arrays;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-16 09:25
 **/
public class Demo19 {

    public static void main(String[] args) {
        int[] code1 = {5,7,1,4};
        int[] code2 = {1,2,3,4};
        int[] code3 = {2,4,9,3};
        new Demo19().decrypt(code1, 3);
        new Demo19().decrypt(code2, 0);
        new Demo19().decrypt(code3, -2);
    }

    public int[] decrypt(int[] code, int k) {
        int[] arr = new int[code.length];
        int len = arr.length;
        if (k == 0) {
            for (int i = 0; i < len; i++) {
                arr[i] = 0;
            }

        } else if (k > 0) {
            for (int i = 0; i < len; i++) {
                int temp = k;
                int sum = 0;
                int codeIndex = i + 1;
                while (temp-- > 0) {
                   sum += code[codeIndex++ % len];
                }
                arr[i] = sum;
            }
        } else {//{2,4,9,3}
            for (int i = 0; i < len; i++) {
                int temp = k;
                int sum = 0;
                int codeIndex = i + k + len;//0+-2+4 2 3       1-2+4 3 0  2-2+4 0 1     3-2+4 1 2
                while (temp++ < 0) { // 0 - 1 + 4 3

                    sum += code[codeIndex++ % len];
                }
                arr[i] = sum;
            }
        }
        System.out.println(Arrays.toString(arr));
        return arr;
    }
}
